//
//  SecondPhotoViewController.m
//  土巴兔
//
//  Created by 千锋 on 15/12/31.
//  Copyright (c) 2015年 IOS1503薛凯. All rights reserved.
//

#import "SecondPhotoViewController.h"
#import "Default.h"
#import "AFNetworking.h"
#import "SecondCollectionViewCell.h"
#import "SecondModel.h"
#import "HMWaterflowLayout.h"
@interface SecondPhotoViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,HMWaterflowLayoutDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
@property (weak, nonatomic) IBOutlet UIButton *jubuBtn;
@property (weak, nonatomic) IBOutlet UIButton *yanseBtn;
@property (weak, nonatomic) IBOutlet UIButton *fenggeBtn;
@property (weak, nonatomic) IBOutlet UIButton *kongjianBtn;
@property (weak, nonatomic) IBOutlet UIView *fenggeView;
@property (weak, nonatomic) IBOutlet UIView *kongjianView;
@property (weak, nonatomic) IBOutlet UIView *yanseView;
@property (weak, nonatomic) IBOutlet UIView *jubuView;

@property (strong,nonatomic)UICollectionView* collectionview;
@property (strong,nonatomic)HMWaterflowLayout* layout;

@property (strong,nonatomic)NSMutableArray* dataArray;
//纪录当前颜色，空间，风格，局部哪一个面板打开
@property (strong,nonatomic)UIView* FlagView;
@property (strong,nonatomic)NSMutableArray* heightArray;
@end

@implementation SecondPhotoViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    _segment.layer.cornerRadius=16;
    _segment.clipsToBounds=YES;
    // Do any additional setup after loading the view from its nib.
    _kongjianView.hidden=YES;
    _jubuView.hidden=YES;
    _yanseView.hidden=YES;
    _fenggeView.hidden=YES;
    _segment.selectedSegmentIndex=1;
    _heightArray=[[NSMutableArray alloc] initWithCapacity:0];
    [self getData];
    [self createCollectionView];
    
}
#pragma mark- Segment
- (IBAction)segmentChange:(UISegmentedControl *)sender {
    switch ([sender selectedSegmentIndex]) {
        case 0:
            [self.navigationController popViewControllerAnimated:NO];
            break;
            
        default:
            break;
    }
}
#pragma mark- 按钮事件
- (IBAction)Pressjubu:(UIButton *)sender {
    if (_FlagView!=_jubuView) {
        _FlagView.hidden=YES;
        _FlagView=_jubuView;
    }
    _jubuView.hidden=!_jubuView.hidden;
}
- (IBAction)Pressyanse:(UIButton *)sender {
    if (_FlagView!=_yanseView) {
        _FlagView.hidden=YES;
        _FlagView=_yanseView;
    }
    _yanseView.hidden=!_yanseView.hidden;
}
- (IBAction)Pressfengge:(UIButton *)sender {
    if (_FlagView!=_fenggeView) {
        _FlagView.hidden=YES;
        _FlagView=_fenggeView;
    }
    _fenggeView.hidden=!_fenggeView.hidden;
}
- (IBAction)Presskongjian:(UIButton *)sender {
    if (_FlagView!=_kongjianView) {
        _FlagView.hidden=YES;
        _FlagView=_kongjianView;
    }
    _kongjianView.hidden=!_kongjianView.hidden;
}
#pragma mark- 创建网格
- (void)createCollectionView{
    _layout=[[HMWaterflowLayout alloc] init];
    _layout.delegate=self;
    _layout.columnsCount=2;
//    _layout.minimumInteritemSpacing=20;
//    _layout.minimumLineSpacing=10;
    
    _collectionview=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 65, DEVICEWIDTH, DEVICEHEIGHT-65) collectionViewLayout:_layout];
    _collectionview.showsVerticalScrollIndicator=NO;
    _collectionview.delegate=self;
    _collectionview.dataSource=self;
    _collectionview.backgroundColor=[UIColor clearColor];
    [_collectionview registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"SecondCollectionViewCell"];
    [self.view insertSubview:_collectionview atIndex:0];
    
}
#pragma mark- 获取数据
- (void)getData{
    _dataArray=[[NSMutableArray alloc] initWithCapacity:0];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString* urlStr=@"http://mobileapi.to8to.com/index.php";
    NSMutableDictionary* paras=[NSMutableDictionary dictionaryWithDictionary:@{@"action":@"list",@"initialize":@"0",@"perPage":@"30",@"style":@"",@"appid":@"15",@"to8to_token":@"5577450_75ff93ea5d8fae3c8a5a0ef1cb6dc1db",@"space":@"",@"systemversion":@"22",@"imei":@"569eb1056c38e1e8",@"appversion":@"3.6.0",@"appostype":@"1",@"color":@"",@"local":@"",@"version":@"2.5",@"uid":@"5577450",@"page":@"1",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"paging":@"true",@"model":@"images"}];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"] forKey:@"uid"];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"] forKey:@"to8to_token"];
    [manager POST:urlStr parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    NSArray* array=[dic objectForKey:@"data"];
        for (NSDictionary* item in array) {
            SecondModel* model=[[SecondModel alloc] initWithDictionary:item];
            [_dataArray addObject:model];
            [_collectionview reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark- 网格协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondCollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"SecondCollectionViewCell" forIndexPath:indexPath];
    [cell relayoutWithModel:_dataArray[indexPath.row]];
//    [_heightArray addObject:@(cell.frame.size.height)];
//    if (indexPath.row%2==0) {
//        
//        CGFloat H=0;
//        for (int i=0; i<indexPath.row; i+=2) {
//            H=H+[_heightArray[i] floatValue];
//        }
//        cell.frame=CGRectMake(cell.frame.origin.x, H, cell.frame.size.width, cell.frame.size.height);
//    }else{
//        CGFloat H2=0;
//        for (int i=1; i<indexPath.row; i+=2) {
//            H2=H2+[_heightArray[i] floatValue];
//        }
//        cell.frame=CGRectMake(cell.frame.origin.x, H2, cell.frame.size.width, cell.frame.size.height);
//    }
    return cell;
}
#pragma mark- 自定义布局协议
- (CGFloat)waterflowLayout:(HMWaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath{
        CGFloat height=([((SecondModel*)_dataArray[indexPath.row]).height floatValue]*width)/[((SecondModel*)_dataArray[indexPath.row]).width  floatValue];
        return height;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat weidth=(DEVICEWIDTH-30)/2;
//    CGFloat height=([((SecondModel*)_dataArray[indexPath.row]).height floatValue]*weidth)/[((SecondModel*)_dataArray[indexPath.row]).width  floatValue];
//    return CGSizeMake(weidth,height);
//}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section%2==0) {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }else{
        return UIEdgeInsetsMake(10, 0, 10, 10);
    }
}
@end
